

package com.mt.mall.controller.good;

import com.mt.common.core.web.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.good.ShoppingCartEditDto;
import com.mt.mall.entity.good.ShoppingCart;
import com.mt.mall.service.good.ShoppingCartService;
import com.mt.mall.service.good.CommodityService;
import com.mt.common.system.service.AttachmentService;
import com.mt.common.system.service.UserService;


@Api(tags = "购物车")
@RestController
@RequestMapping("/api/mall/good/ShoppingCart")
@CrossOrigin(allowCredentials = "true")
public class ShoppingCartController {
    private static Logger logger = LoggerFactory.getLogger(ShoppingCartController.class);


    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private UserService userService;

    /**
     * 根据分页参数查询购物车集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:view')")
    @ApiOperation("分页查询购物车")
    @ApiPageParam
    @PostMapping("/findShoppingCarts")
    public PageResultDTO findShoppingCarts(@RequestBody PageDTO pageDTO) {
        return this.shoppingCartService.findShoppingCarts(pageDTO);
    }

    @ApiOperation("分页查询购物车")
    @ApiPageParam
    @PostMapping("/findUserShoppingCarts")
    public PageResultDTO findUserShoppingCarts(@RequestBody PageDTO pageDTO) {
        return this.shoppingCartService.findUserShoppingCarts(pageDTO);
    }
    /**
     * 根据ID查询指定的购物车
     *
     * @param shoppingCartId Id
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:view')")
    @ApiOperation("根据id查询购物车")
    @ApiPageParam
    @PostMapping("/findShoppingCart")
    public ShoppingCart findShoppingCart(@RequestParam Long shoppingCartId) {
        return this.shoppingCartService.findShoppingCart(shoppingCartId);
    }

    /**
     * 根据ID查询指定的购物车(包含外键名称)
     *
     * @param shoppingCartId Id
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:view')")
    @ApiOperation("根据ID查询指定的购物车(包含外键名称)")
    @PostMapping("/findShoppingCartForView")
    public ShoppingCart findShoppingCartForView(@RequestParam Long shoppingCartId) {
        return this.shoppingCartService.findShoppingCartWithForeignName(shoppingCartId);
    }

    /**
     * 根据ID查询指定的购物车(包含购物车和外键名称)
     *
     * @param shoppingCartId Id
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:view')")
    @ApiOperation("根据ID查询指定的购物车(包含购物车和外键名称)")
    @PostMapping("/findShoppingCartForEdit")
    public ShoppingCartEditDto findShoppingCartForEdit(@RequestParam Long shoppingCartId) {
        ShoppingCartEditDto shoppingCartEditDto = new ShoppingCartEditDto();
        shoppingCartEditDto.setShoppingCart(this.shoppingCartService.findShoppingCartWithForeignName(shoppingCartId));

        this.prepareShoppingCartEditDto(shoppingCartEditDto);

        return shoppingCartEditDto;
    }

    /**
     * 根据ID查询指定的购物车(只提取ID 和 Name)
     *
     * @param shoppingCartId Id
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:view')")
    @ApiOperation("根据ID查询指定的购物车(只提取ID 和 Name)")
    @PostMapping("/findShoppingCartsWithIdNameById")
    public ShoppingCart findShoppingCartsWithIdNameById(@RequestParam Long shoppingCartId) {
        return this.shoppingCartService.findShoppingCartsWithIdNameById(shoppingCartId);
    }

    /**
     * 根据名称查询购物车集合(只提取ID 和 Name)
     *
     * @param shoppingCartName 名称
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:view')")
    @ApiOperation("根据名称查询购物车集合(只提取ID 和 Name)")
    @PostMapping("/findShoppingCartsWithIdNameByName")
    public List<ShoppingCart> findShoppingCartsWithIdNameByName(@RequestParam String shoppingCartName) {
        return this.shoppingCartService.findShoppingCartsWithIdNameByName(shoppingCartName);
    }


    /**
     * 创建新的购物车
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:add')")
    @ApiOperation("创建新的购物车")
    @PostMapping("/createShoppingCart")
    public ShoppingCartEditDto createShoppingCart() {
        ShoppingCartEditDto shoppingCartEditDto = new ShoppingCartEditDto();
        shoppingCartEditDto.setShoppingCart(new ShoppingCart());

        this.prepareShoppingCartEditDto(shoppingCartEditDto);
        return shoppingCartEditDto;
    }

    private void prepareShoppingCartEditDto(ShoppingCartEditDto shoppingCartEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        shoppingCartEditDto.setCommodityCommoditys(this.commodityService.findAllCommoditysWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        shoppingCartEditDto.setAttachmentsAttachments(this.attachmentService.findAllAttachmentsWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        shoppingCartEditDto.setUserUsers(this.userService.findAllUsersWithIdName());
    }

    /**
     * 新增保存购物车
     *
     * @param shoppingCart 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:add')")
    @ApiOperation("新增保存购物车")
    @PostMapping("/saveShoppingCart")
    public ShoppingCart saveShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart.setStatus("0");
        return this.shoppingCartService.saveShoppingCart(shoppingCart);
    }
    //@PreAuthorize("hasAuthority('mall:good:ShoppingCart:add')")
    @ApiOperation("用户新增保存购物车")
    @PostMapping("/addShoppingCart")
    public ShoppingCart addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        return this.shoppingCartService.addShoppingCart(shoppingCart);
    }
    /**
     * 修改保存购物车
     *
     * @param shoppingCart 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:update')")
    @ApiOperation("修改保存购物车")
    @PostMapping("/updateShoppingCart")
    public ShoppingCart updateShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        return this.shoppingCartService.updateShoppingCart(shoppingCart);
    }

    /**
     * 根据ID删除购物车
     *
     * @param shoppingCartId ID
     */
    @PreAuthorize("hasAuthority('mall:good:ShoppingCart:remove')")
    @ApiOperation("根据ID删除购物车")
    @PostMapping("/deleteShoppingCart")
    public void deleteShoppingCart(@RequestParam Long shoppingCartId) {
        this.shoppingCartService.deleteShoppingCart(shoppingCartId);
    }
    @ApiOperation("根据ID删除购物车")
    @PostMapping("/delShoppingCart")
    public void delShoppingCart(@RequestBody List<Long> shoppingCartIds) {
        this.shoppingCartService.delShoppingCart(shoppingCartIds);
    }


    @ApiOperation("更新count")
    @PostMapping("/updateCount")
    public JsonResult updateCount(@Param("shoppingCartId")Long shoppingCartId, @Param("count") Integer count) {
       return this.shoppingCartService.updateCount(shoppingCartId,count);
    }
    @ApiOperation("结算")
    @PostMapping("/settlement")
    public JsonResult settlement(@Param("shoppingCarId")Long shoppingCarId) {
        return this.shoppingCartService.settlement(shoppingCarId);
    }



}

